<!DOCTYPE HTML>
<html lang="zh-CN">

<head><meta name="generator" content="Hexo 3.9.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="ydong博客">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <meta name="theme-version" content="1.2.3">
    <meta name="root" content="/blog/">
    <link rel="dns-prefetch" href="http://ydongabc.gitee.io/blog">
    <!--SEO-->

<meta name="keywords" content="nodeJS基础">


<meta name="description" content="1.file-system文件模块12345678910111213141516171819const fs = require('fs'); // 1.读取fs.readFile(" . 01....">


<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">
    <!--Title-->

<title>
    
    nodeJS基础 |
    
    ydong博客
</title>

<link rel="alternate" href="/atom.xml" title="ydong博客" type="application/atom+xml">


<link rel="icon" href="./favicon.ico">

    

<link rel="stylesheet" href="/blog/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/blog/css/font-awesome.min.css?rev=4.7.0">
<link rel="stylesheet" href="/blog/css/style.css?rev=@@hash">
    
<div class="hide">
    <script type="text/javascript">
    var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");
    document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
    </script>
</div>




    

</head></html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->
<body>
    <header class="main-header"  style="background-image:url(
    /blog/./img/banner.jpg)"
     >
    <div class="main-header-box">
        <a class="header-avatar" href="/" title='John Doe'>
            <img src="/blog/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
            <!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
            <img src="/blog/img/branding.png" alt="Snippet 博客主题" class="img-responsive center-block">
            
        </div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                        <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="http://ydongabc.gitee.io/blog">
                        ydong博客</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog"><i class="fa "></i>
                                首页</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/前端/"><i class="fa "></i>
                                前端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/后端/"><i class="fa "></i>
                                后端</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/categories/工具/"><i class="fa "></i>
                                工具</a>
                        </li>
                        
                        <li role="presentation" class="text-center">
                            <a href="/blog/archives/"><i class="fa "></i>
                                时间轴</a>
                        </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="nodeJS基础">
            
            nodeJS基础
            
        </h1>
        <div class="post-meta">
    
    <span class="categories-meta fa-wrap">
        <i class="fa fa-folder-open-o"></i>
        <a class="category-link" href="/blog/categories/后端/">后端</a> <a class="category-link" href="/blog/categories/后端/nodeJS/">nodeJS</a>
    </span>
    
    
    <span class="fa-wrap">
        <i class="fa fa-tags"></i>
        <span class="tags-meta">
            
            <a class="tag-link" href="/blog/tags/nodeJS基础/">nodeJS基础</a>
            
        </span>
    </span>
    
    
    
    <span class="fa-wrap">
        <i class="fa fa-clock-o"></i>
        <span class="date-meta">
            2019/09/20</span>
    </span>
    
    
</div>
        
        
    </div>
    
    <div class="post-body post-content">
        <h2 id="1-file-system文件模块"><a href="#1-file-system文件模块" class="headerlink" title="1.file-system文件模块"></a>1.file-system文件模块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">// 1.读取</span></span><br><span class="line">fs.readFile(<span class="string">"./01.txt"</span>,<span class="function"><span class="keyword">function</span>(<span class="params">error,data</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(error)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"读取失败"</span>+error)</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(data.toString()) <span class="comment">//成功</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2.写入</span></span><br><span class="line">fs.writeFile(<span class="string">"./01.txt"</span>,<span class="string">"这是内容"</span>,<span class="function"><span class="keyword">function</span>(<span class="params">err</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(err)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"写入失败"</span>+err)</span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"成功"</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="2-http服务模块"><a href="#2-http服务模块" class="headerlink" title="2.http服务模块"></a>2.http服务模块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1.创建服务器实例</span></span><br><span class="line"><span class="keyword">let</span> server = http.createServer();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 2.接收请求(域名下所有的url都会触发)</span></span><br><span class="line">server.on(<span class="string">'request'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">req,res</span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">"成功接收到客户端的请求，请求路径是："</span>+req.url)</span><br><span class="line">  <span class="keyword">let</span> url = req.url;</span><br><span class="line">  <span class="keyword">let</span> data = &#123;<span class="attr">str</span>:<span class="string">"数据"</span>&#125;;</span><br><span class="line">  res.setHeader(<span class="string">'Content-Type'</span>,<span class="string">'text/plain;charset=utf-8'</span>) <span class="comment">//设置编码格式</span></span><br><span class="line">  <span class="keyword">if</span>(url === <span class="string">'/'</span>)&#123;</span><br><span class="line">    res.end(<span class="string">'这是首页'</span>)</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(url === <span class="string">'/login'</span>)&#123;</span><br><span class="line">    res.end(<span class="built_in">JSON</span>.stringify(data))  <span class="comment">//只能发送二进制数据或者字符串</span></span><br><span class="line">  &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    res.end(<span class="string">'404 No Fount'</span>)</span><br><span class="line">  &#125;</span><br><span class="line"><span class="comment">//  res.write("写入数据内容，可以多次写入")</span></span><br><span class="line"><span class="comment">//  res.write("hello world")</span></span><br><span class="line"><span class="comment">//  res.end()  // 发送数据并结束响应，必需，只能一次，结束后代码后面的操作将会无法执行</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3.绑定端口，启动服务器</span></span><br><span class="line">server.listen(<span class="number">8090</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">"启动成功"</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 4.简写</span></span><br><span class="line">http.createServer(<span class="function"><span class="keyword">function</span>(<span class="params">req,res</span>)</span>&#123;&#125;).listen(<span class="number">8090</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="3-url模块"><a href="#3-url模块" class="headerlink" title="3.url模块"></a>3.url模块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> url = <span class="built_in">require</span>(<span class="string">'url'</span>);</span><br><span class="line">url.parse(<span class="string">"http://www.cyd.com:3000/test/p?name=cyd&amp;age=18#hash"</span>, <span class="literal">true</span>);</span><br><span class="line"><span class="comment">// 返回值：</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">"protocol"</span>: <span class="string">"http:"</span>,</span><br><span class="line">    <span class="string">"slashes"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="string">"auth"</span>: <span class="literal">null</span>,</span><br><span class="line">    <span class="string">"host"</span>: <span class="string">"www.cyd.com:3000"</span>,</span><br><span class="line">    <span class="string">"port"</span>: <span class="string">"3000"</span>,</span><br><span class="line">    <span class="string">"hostname"</span>: <span class="string">"www.cyd.com"</span>,</span><br><span class="line">    <span class="string">"hash"</span>: <span class="string">"#hash"</span>,</span><br><span class="line">    <span class="string">"search"</span>: <span class="string">"?name=cyd&amp;age=18"</span>,</span><br><span class="line">    <span class="string">"query"</span>: &#123; <span class="string">"name"</span>: <span class="string">"cyd"</span>, <span class="string">"age"</span>: <span class="string">"18"</span> &#125;,</span><br><span class="line">    <span class="string">"pathname"</span>: <span class="string">"/test/p"</span>,</span><br><span class="line">    <span class="string">"path"</span>: <span class="string">"/test/p?name=cyd&amp;age=18"</span>,</span><br><span class="line">    <span class="string">"href"</span>: <span class="string">"http://www.cyd.com:3000/test/p?name=cyd&amp;age=18#hash"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-require和exports模块系统"><a href="#4-require和exports模块系统" class="headerlink" title="4.require和exports模块系统"></a>4.require和exports模块系统</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.a.js文件:</span></span><br><span class="line">exports.data = <span class="string">"数据a"</span>;</span><br><span class="line">exports.add = <span class="function"><span class="keyword">function</span>(<span class="params">x,y</span>)</span>&#123; <span class="keyword">return</span> x+y &#125;;</span><br><span class="line"><span class="comment">// 只导出单个成员时需用 module.exports = xxx</span></span><br><span class="line"><span class="comment">// 2.b.js文件:</span></span><br><span class="line"><span class="keyword">let</span> obj = <span class="built_in">require</span>(<span class="string">'./a.js'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(obj.data); <span class="comment">// &#123;data: "s数据a"&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.add(<span class="number">1</span>,<span class="number">2</span>)); <span class="comment">// 3</span></span><br></pre></td></tr></table></figure>


    </div>
    
    <div class="post-footer">
        <div>
            
            转载声明：
            商业转载请联系作者获得授权,非商业转载请注明出处 © <a href="" target="_blank">Snippet</a>
            
            
        </div>
        <div>
            
        </div>
    </div>
</article>
<div class="article-nav prev-next-wrap clearfix">
    
    <a href="/blog/2019/09/21/nodeJS实现get和post请求接口/" class="pre-post btn btn-default" title='nodeJS实现get和post请求接口'>
        <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
        <span class="hidden-xs">
            nodeJS实现get和post请求接口</span>
    </a>
    
    
    <a href="/blog/2019/09/19/webpack的安装和基本命令/" class="next-post btn btn-default" title='webpack的安装和基本命令'>
        <span class="hidden-lg">下一篇</span>
        <span class="hidden-xs">
            webpack的安装和基本命令</span><i class="fa fa-angle-right fa-fw"></i>
    </a>
    
</div>

                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">
            文章目录
        </h3>
        
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-file-system文件模块"><span class="toc-text">1.file-system文件模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-http服务模块"><span class="toc-text">2.http服务模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-url模块"><span class="toc-text">3.url模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-require和exports模块系统"><span class="toc-text">4.require和exports模块系统</span></a></li></ol>
        
    </div>
</aside>
                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>
<a id="back-to-top" class="icon-btn hide">
    <i class="fa fa-chevron-up"></i>
</a>
    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>
            </div>
            <div class="col-sm-12">
                <span>Copyright &copy;
                    2017
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>



<script src="/blog/js/app.js?rev=@@hash"></script>
</body>
</html>